package m.jclecai.com.activity;

import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import m.jclecai.com.R;
import m.jclecai.com.activity.follow.HotOrderDetailActivity;
import m.jclecai.com.adapter.ExpandGroupBuyInfoAdapter;
import m.jclecai.com.adapter.OrderFollowPeopleAdapter;
import m.jclecai.com.bean.GroupBuyBean;
import m.jclecai.com.bean.GroupBuyDetailBean;
import m.jclecai.com.bean.Response.ResFollowOrederPeopleList;
import m.jclecai.com.bean.Response.ResHotOrederDetail;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.listener.OnItemClickListener;
import m.jclecai.com.utils.BaseUtils;
import m.jclecai.com.utils.ChangeUtils;
import m.jclecai.com.utils.DateUtil;
import m.jclecai.com.utils.PreferenceUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.view.CustomExpandListview;


/**
 * Created by ZD on 2017/5/15.
 * 购买记录
 */

public class BuyDetailActivity extends LotteryRecordActivity implements OnItemClickListener {

    private OrderFollowPeopleAdapter mHotPeopleListAdapter;

    @Override
    public int getContentView() {
        return R.layout.activity_buy_detail;
    }

    @Override
    public void initView() {
        super.initView();
        /**
         * 分为自购、跟单、神单
         */
        datas = new ArrayList<>();
        expand_list = (CustomExpandListview) findViewById(R.id.expand_list);
        expand_list.addHeaderView(View.inflate(this, R.layout.header_buy_detail_version_2, null));
        expand_list.addFooterView(View.inflate(this, R.layout.footer_group_buy_detail, null));
        //覆盖BaseActivity的引用 因为使用了 View.inflate 所以必须等到全部加载了在绑定 能够得到引用
        ButterKnife.bind(this);
        setTitle("自购详情");
        leftOptionText("");
        //总进球
        adapter = new ExpandGroupBuyInfoAdapter(this, expand_list, datas, this);
        expand_list.setAdapter(adapter);
        //悬浮的标题
        expand_list.setHeaderView(getLayoutInflater().inflate(
                R.layout.item_group_buy_parent, expand_list, false));
        expand_list.findViewById(R.id.tv_detail).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (groupBean != null) {
                    startActivity(new Intent(BuyDetailActivity.this, OrderDetailActivity.class)
                            .putExtra("id", groupBean.getId()));
                }
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        String str = getIntent().getStringExtra("bean");
        if (!TextUtils.isEmpty(str))
            groupBean = FastOk.get(str, GroupBuyBean.class);
        userBean = PreferenceUtil.readUser(this);
        String strDetail = getIntent().getStringExtra("detaigroup_statuslBean");
        if (TextUtils.isEmpty(strDetail))
            getData(Constant_Url.SELF_DETAIL);
        else {
            detailBean = FastOk.get(strDetail, GroupBuyDetailBean.class);
            execData();
            adapter.notifyDataSetChanged();
            for (int i = 0; i < adapter.getGroupCount(); i++) {// 默认全部展开
                expand_list.expandGroup(i);
            }
        }
        if (ivIcon != null)
            ivIcon.setImageResource(getIcon());
        updateUIFollowList();
    }

    private List<ResFollowOrederPeopleList.FollowPeopleBean> arrayList;
    //更新用户列表

    /**
     * 更新页面
     */
    public void updateUIFollowList() {
        ll_follow_list.setVisibility(View.GONE);
        String html = "跟单列表(" + 0 + "人 , " +
                "跟单金额:" + 0 + "元)";
        tv_follow_info_title.setText(Html.fromHtml(html));
        //显示跟单列表
        arrayList = new ArrayList<>();
        arrayList.add(new ResFollowOrederPeopleList.FollowPeopleBean());
        arrayList.add(new ResFollowOrederPeopleList.FollowPeopleBean());
        arrayList.add(new ResFollowOrederPeopleList.FollowPeopleBean());
        arrayList.add(new ResFollowOrederPeopleList.FollowPeopleBean());
        mHotPeopleListAdapter = new OrderFollowPeopleAdapter(this, arrayList);
        mPtrListView.setAdapter(mHotPeopleListAdapter);
    }

    @Override
    protected void updateText(GroupBuyDetailBean bean) {
        String Str = "";
        //截止时间
//        BaseUtils.saleEnd(tvHeadEndTime, DateUtil.strToLong(bean.getEndtime()), 1000);
        if (tvHeadEndTime != null)
            tvHeadEndTime.setText(bean.getResult());
        if (tvHeadEndTimeDesc != null)
            tvHeadEndTimeDesc.setVisibility(View.GONE);
        //中奖金额；
        if (tv_jiangjin != null) {
//            tv_follow_number.setText(bean.getFollow_count() + "人");
//            tv_yongjin.setText(bean.getTeambuy_money() + "元");
            if (!TextUtils.isEmpty(bean.getWin_money()) && Float.parseFloat(bean.getWin_money()) > 0) {
                String awardStr = bean.getWin_money() + "元";
                String win_money = bean.getWin_money();
                DecimalFormat df = new DecimalFormat("#########.#");
                Str = df.format(Float.parseFloat(win_money) - bean.getWin_addmoney());
                if (bean.getWin_addmoney() > 0) {
                    awardStr += " ( 加奖: " + bean.getWin_addmoney() + "元 )";
                }
                tv_jiangjin.setText(awardStr);
                tv_jiangjin.setVisibility(View.VISIBLE);
//                table2.setVisibility(View.VISIBLE);
//                ll_weizhongjiang.setVisibility(View.GONE);
            } else {
//                ll_weizhongjiang.setVisibility(View.VISIBLE);
//                table2.setVisibility(View.GONE);
//                tvLotteryAward.setVisibility(View.GONE);
//                tvLotteryAwardMoney.setVisibility(View.GONE);
            }

        }
        //用户昵称图像
        if (ivIcon != null) {
            Glide.with(this)
                    .load(userBean.getUser_face())
                    .asBitmap()
                    .placeholder(R.drawable.my_head_de)
                    .error(R.drawable.my_head_de)
                    .into(ivIcon);
        }
        //开奖号码
        if (tvLottery != null && !TextUtils.isEmpty(bean.getOpencodes())) {
            if (TextUtils.isEmpty(Str)) {
                tvLottery.setText((bean.getOpencodes().replaceAll(",", " ")));
            } else {
                double str = Double.parseDouble(Str);
                tvLottery.setText(ChangeUtils.doubleTo2(str) + "元");
            }
        }
        //投注金额
        if (tv_group_buy_money != null)
            tv_group_buy_money.setText(bean.getOne_money());
        //彩种 old  new:用户昵称
        if (tvTitle != null)
            tvTitle.setText(bean.getUsername());
        //期号
        if (tvNum != null)
            tvNum.setText("第" + bean.getTermNo() + "期");
        //订单编号
        if (tvOrder != null)
            tvOrder.setText(bean.getPlanNo());
        //方案宣言
        if (tvDesc != null)
            tvDesc.setText(bean.getTeambuy_content());
        //下单时间
        if (tvTime != null)
            tvTime.setText(bean.getAddtime());
        //截止时间
        if (tvFooterEndtime != null)
            tvFooterEndtime.setText(bean.getEndtime());
        //备注 中奖了显示跟单于谁
        if (tv_beizhu_or_follow != null) {
//            tv_beizhu_or_follow.setText(bean.getti);
        }
        //怎么领奖
        if (tvGetGift != null)
            tvGetGift.setText("如中奖，彩金直接返还至购彩账户");
    }

    @Override
    public void onItemClick(int position) {

    }
}
